/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.web.product;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.hk.shop.entity.brand.HkShopBrand;
import com.thinkgem.jeesite.modules.hk.shop.entity.product.HkShopProduct;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkUserShop;
import com.thinkgem.jeesite.modules.hk.shop.service.brand.HkShopBrandService;
import com.thinkgem.jeesite.modules.hk.shop.service.product.HkShopProductService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 乐购商品Controller
 *
 * @author 罗天文
 * @version 2018-05-16
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/product/hkShopProduct")
public class HkShopProductController extends BaseController {

    @Autowired
    private HkShopProductService hkShopProductService;
    @Autowired
    private HkShopBrandService hkShopBrandService;

    @ModelAttribute
    public HkShopProduct get(@RequestParam(required = false) String id) {
        HkShopProduct entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = hkShopProductService.get(id);
        }
        if (entity == null) {
            entity = new HkShopProduct();
        }
        return entity;
    }


    @RequiresPermissions("shop:product:hkShopProduct:view")
    @RequestMapping(value = {"list", ""})
    public String list(HkShopProduct hkShopProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
        HkUserShop hkUserShop = new HkUserShop();
        hkUserShop.setId(UserUtils.getShopId());
        hkShopProduct.setShop(hkUserShop);
        model.addAttribute("brands", hkShopBrandService.findList(new HkShopBrand()));
        Page<HkShopProduct> page = hkShopProductService.findPage(new Page<HkShopProduct>(request, response), hkShopProduct);
        model.addAttribute("page", page);
        return "hk/shop/product/hkShopProductList";
    }

    @RequiresPermissions("shop:product:hkShopProduct:view")
    @RequestMapping(value = "form")
    public String form(HkShopProduct hkShopProduct, Model model) {
        model.addAttribute("brands", hkShopBrandService.findList(new HkShopBrand()));
        model.addAttribute("hkShopProduct", hkShopProduct);
        return "hk/shop/product/hkShopProductForm";
    }

    @RequiresPermissions("shop:product:hkShopProduct:edit")
    @RequestMapping(value = "save")
    public String save(HkShopProduct hkShopProduct, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request) {
        if (!beanValidator(model, hkShopProduct)) {
            return form(hkShopProduct, model);
        }

        if (StringUtils.isBlank(UserUtils.getShopId())) {
            addMessage(model, "登录超时，请退出重新登录。");
            return "redirect:" + Global.getFrontPath();
        }


        if (StringUtils.isNotBlank(hkShopProduct.getId())) {
            if (!hkShopProduct.getShop().getId().equals(UserUtils.getShopId())) {
                addMessage(model, "非本人操作，请退出重新登录！");
                return form(hkShopProduct, model);
            }
        }


        HkUserShop hkUserShop = new HkUserShop();
        hkUserShop.setId(UserUtils.getShopId());
        hkShopProduct.setShop(hkUserShop);
		/*if(null==hkShopProduct.getHkShopProductPropertyList()||hkShopProduct.getHkShopProductPropertyList().size()==0){
			addMessage(model, "商品属性必须选择");
			return form(hkShopProduct, model);
		}*/
       /* if (StringUtils.isEmpty(hkShopProduct.getColors())) {
            addMessage(model, "商品sku颜色必须选择");
            return form(hkShopProduct, model);
        }
        if (StringUtils.isEmpty(hkShopProduct.getSpecs())) {
            addMessage(model, "商品sku规格必须选择");
            return form(hkShopProduct, model);
        }*/
        /*if (null == hkShopProduct.getHkShopProductPropertyList() || hkShopProduct.getHkShopProductPropertyList().size() == 0) {
            addMessage(model, "商品属性必须选择");
            return form(hkShopProduct, model);
        }*/
        if (null == hkShopProduct.getHkShopProductImagesList() || hkShopProduct.getHkShopProductImagesList().size() == 0) {
            addMessage(model, "产品图片必须选择");
            return form(hkShopProduct, model);
        }
        hkShopProductService.save(hkShopProduct);
        addMessage(redirectAttributes, "保存乐购商品成功");
        return "redirect:" + Global.getAdminPath() + "/shop/product/hkShopProduct/?repage";
    }

    @RequiresPermissions("shop:product:hkShopProduct:edit")
    @RequestMapping(value = "delete")
    public String delete(HkShopProduct hkShopProduct, RedirectAttributes redirectAttributes) {
        hkShopProductService.delete(hkShopProduct);
        addMessage(redirectAttributes, "删除乐购商品成功");
        return "redirect:" + Global.getAdminPath() + "/shop/product/hkShopProduct/?repage";
    }

}